package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.BeanDto;
import com.itheima.dto.AdvposSDto;
import com.itheima.entity.YddAdvimg;
import com.itheima.entity.YddAdvpos;
import com.itheima.service.YddAdvimgService;
import com.itheima.service.YddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advpos")
public class AdvposController {

    @Autowired
    private YddAdvposService advposService;

    @Autowired
    private YddAdvimgService advimgService;

    @GetMapping("/list")
    public R<BeanDto<AdvposSDto>> list(String advposdesc , String advposname, int page, int limit){
        //构建分页条件
        Page<YddAdvpos> pageinfo = new Page<>(page,limit);
        //构建条件查询器
        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(advposname),YddAdvpos::getAdvposname,advposname);

        //查询
        advposService.page(pageinfo,lqw);
        //将得到的数据装进dto中
        //当前页
        long current = pageinfo.getCurrent();

        BeanDto<AdvposSDto> advposDto = new BeanDto();
        //存入得到的数据
        List<YddAdvpos> records = pageinfo.getRecords();

        //因为返回的数据需要统计有多少广告 所以需要dto
        List<AdvposSDto> list = records.stream().map(item ->{
            AdvposSDto advposSDto = new AdvposSDto();
            BeanUtils.copyProperties(item,advposSDto);
            LambdaQueryWrapper<YddAdvimg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(YddAdvimg::getAdvimgpos,item.getId());
            int count = advimgService.count(lambdaQueryWrapper);
            advposSDto.setAdvposnum(count);
            return advposSDto;
        }).collect(Collectors.toList());


        advposDto.setData(list);
        //当前页
        advposDto.setCurrentPage(pageinfo.getCurrent());
        //记录的总数
        advposDto.setCount(pageinfo.getTotal());
        //每页大小
        advposDto.setPoageSize((long)limit);
        //总共多少页
        advposDto.setTotalPages(pageinfo.getTotal()/limit + 1);


        return R.success(advposDto);
    }

    @GetMapping("/del")
    public R<String> del(int id){

        //直接根据id删除
        boolean b = advposService.removeById(id);
        if (b){
            //删除成功
            return R.success("");
        }
        //删除失败
        return null;

    }



    //添加需求
    @PostMapping("/add")
    public R add(@RequestBody YddAdvpos advpos){

        advposService.save(advpos);

        return R.success("");

    }
    //查看单个标签
    @GetMapping("/rowInfo")
    public R<YddAdvpos> getrowInfo(int id){
        YddAdvpos byId = advposService.getById(id);
        return R.success(byId);
    }

    //修改标签
    @PostMapping("/update")
    public R update(@RequestBody YddAdvpos tag){
        advposService.updateById(tag);
        return R.success("");

    }

}
